package com.example.bms.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bms.entity.ApiResult;
import com.example.bms.entity.Teacher;
import com.example.bms.serviceimpl.TeacherServiceImpl;
import com.example.bms.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @apiNote 教师的增删改查
 * @author
 * @email 1627856348@qq.com
 * @since 1.0.0
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
public class TeacherController {
    private TeacherServiceImpl teacherService;
    @Autowired
    public TeacherController(TeacherServiceImpl teacherService){
        this.teacherService = teacherService;
    }


    /**
     * @apiNote 查询所有教师
     * @param page 页数
     * @param size 条数
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size){
        Page<Teacher> teacherPage = new Page<>(page,size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);

        return ApiResultHandler.buildApiResult(200,"查询所有教师",teacherIPage);
    }


    /**
     * @apiNote 根据teacherId查询某一教师所有信息
     * @param teacherId 教师id
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/teacher/{teacherId}")
    public ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }


    /**
     * @apiNote 根据teacherId删除教师
     * @param teacherId 教师id
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        return ApiResultHandler.success(teacherService.deleteById(teacherId));
    }


    /**
     * @apiNote 修改教师信息
     * @param teacher 教师实体类
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher){
        System.out.println(1);
        return ApiResultHandler.success(teacherService.update(teacher));
    }


    /**
     * @apiNote 添加教师
     * @param teacher 教师实体类
     * @author 
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher){
        return ApiResultHandler.success(teacherService.add(teacher));
    }
}
